package com.ykt.controller;

import com.jfinal.ext.route.ControllerBind;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.ykt.model.Admin;
import com.ykt.model.GoodsCategory;

import java.util.List;
import java.util.Map;

@ControllerBind(controllerKey="/goodsCategory")
public class GoodsCategoryController extends BaseController<GoodsCategory> {

	public GoodsCategoryController() {
		super(GoodsCategory.class);
	}
	
	@Override
	public void grid() {
        int page = getParaToInt("page", 1);
        int rows = this.getParaToInt("rows", 20);
        
        Admin admin = getCurrentUser();
        Map<String, String> params = this.toParaMap();
        if(admin.get(Admin.role_ids)!=null && admin.getStr(Admin.role_ids).equals("6")){
        	params.put("EQ_s.id",admin.getInt(Admin.shopId).toString());
        }
        
        try {
            Page<GoodsCategory> data = GoodsCategory.dao.pageFindModel(page, rows, params);
            rendDataGrid(data);
        } catch (Exception e) {
            log.error(e);
            rendJson(false, e.getMessage());
        }
    }
	
	/**查找某一商铺下的商品分类*/
	public void comboboxShopCategory() {
        int id = getParaToInt(0, 0);

        List<Record> list = null;
        String sql = "SELECT * FROM goods_category ";
        
        if (id == 0){
        	Admin admin = getCurrentUser();
        	if(admin.get(Admin.role_ids)!=null && admin.getStr(Admin.role_ids).equals("6")){
        		sql = sql + " where shop_id="+admin.getInt(Admin.shopId)+"";
        	}

        	list = Db.find(sql);
        }
        else list = Db.find(sql + " where shop_id = ?", id);

        this.renderJson(list);
    }
	
}
